//现有的api有
// sendAct能获得活动数据
// sendPolicy获得政策数据
// Soilder获得大学生退伍兵数据
// Org获得社团信息
// Stu获得普通学生信息
// Post帖子信息
// volunteer志愿者信息
//searchMsg搜索信息，带后端完善后改为传参
// 格式见db.json

import { reactive } from "vue";
import axios from "axios"
import { ElMessage } from "element-plus";
const store = {
  state: reactive({
    //活动数组act,使用sendAct解构后为数组对象
    act:[],
    policy:[],
    //学生（组织）类型判断 退伍大学生：军人 普通大学生：学生 军事社团 ：组织
    StuType:"组织",
    Stu:{},
    Student:{},
    Soldier:{},
    Org:{},
    searchmsg:{},
    policytype: '',
    volunteer: {},
    Posts:{},
    ProFile_url:"",
    //测试数组
    arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

  }),
  //在入口页已经调用过此函数，不要重复调用
  sendAct2(){
   return axios({
      // url:'http://localhost:3000/sendAct',
	      url:'http://8.130.46.21:8000/act/list/',
      method: 'get',
  })
  
  },

  sendAct(){
     axios({
      // url:'http://localhost:3000/sendAct',
      url:'http://8.130.46.21:8000/act/list/',
       method: 'get',
   }).then(res => {
    this.state.act=res.data
    // console.log(this.act) 
    console.log("res.data")
})
   
   },
  getStu(){
  //   axios({
  //     // url:'http://localhost:3000/Soilder?schNo=2213712861',
  //     url:'http://localhost:3000/Org?orgName=军事天天看', 
  //     method: 'get',
  // }).then(res => {
  //     this.state.Stu=res.data
  //     // console.log(this.act) 
  //     console.log("res.data")
  // })
        this.state.Stu[0]=JSON.parse(localStorage.getItem('user'));
  },
  changetype(str){
    this.state.policytype=str
    console.log("ok")
    console.log(this.state.policytype)
    console.log(str)
  },
  sendPolicy(){
    axios({

      url:'http://8.130.46.21:8000/policy/list/',
      // url:'http://localhost:3000/sendPolicy',
      method: 'get',
  }).then(res => {

      this.state.policy=res.data
      // console.log(this.act) 
      console.log("ok")
  })
  },
  
  sendPolicy2(){
  return  axios({
    url:'http://8.130.46.21:8000/policy/list/',
    method: 'get',  })   
  }, 
  sendVolunteer(){
    axios({
      // url:'http://localhost:3000/volunteer',
      url:'http://8.130.46.21:8000/volunteer/list/',
      method: 'get',
  }).then(res => {

      this.state.policy=res.data
      // console.log(this.act) 
      console.log("ok")
  })
  },
  sendVolunteer2(){
    return  axios({
      //  url:'http://localhost:3000/volunteer',
      url:'http://8.130.46.21:8000/volunteer/list/',
        method: 'get',
    })
  },
  searchMsg(msg){
    axios({
    //  url:'http://localhost:3000/searchMsg',
      url:'http://8.130.46.21:8000/search/list/?s='+msg,
      method: 'get',
  }).then(res => {
      this.state.searchmsg=res.data
      // console.log(this.act) 
      console.log("ok")
  })
  },getstu(){
    axios({
      url:'http://8.130.46.21:8000/stu/info/',
      method: 'get',
    }).then(res=>{
      this.state.Student=res.data;
    }
    )
  },getsoldier(){
    axios({
      url:'http://8.130.46.21:8000/soldier/info/',
      method: 'get',
    }).then(res=>{
      this.state.Soldier=res.data;
    }
    )
  },getorg(){
    axios({
      url:'http://8.130.46.21:8000/org/info/',
      method: 'get',
    }).then(res=>{
      this.state.Org=res.data;
    }
    )
  }

  ,getPosts(){
    axios({
      // url:'http://localhost:3000/Post',
      url:'http://8.130.46.21:8000/post/list/',
      method: 'get',
  }).then(res => {
      this.state.Posts=res.data
      // console.log(this.act) 
      console.log(this.state.Posts)
  })
  },getProfile(schNo){
    var schNoFormdata=new FormData();
      schNoFormdata.append("sch_No",schNo);
      axios({
      url: 'http://8.130.46.21:8000/profile/address/',
      method: 'post',
      data: schNoFormdata
    }).then(res=>{
      console.log(res.data.profile)
      this.state.ProFile_url=res.data.profile
    }
    )

  },
  postPosts(post){
   
    axios({
      url: 'http://8.130.46.21:8000/post/add/',
      method: 'post',
      data: post
    }).then(res=>{
      ElMessage({
        message: "发帖成功",
        type: "success",
      });
    }
    ).catch(res=>{
      ElMessage({
        message: "发帖失败，请重试",
        type: "error",
      });
    })
  },patchThumbs(num,id){
    var thumbsForm=new FormData();
    var index=this.state.Posts.find(function (obj) {
      return obj.id==id
    })
    index.thumb=num;
    thumbsForm.append("id",id);
    thumbsForm.append("thumb",num);
    axios({
      url: 'http://8.130.46.21:8000/thumb/good/',
      method: 'post',
      data: thumbsForm
    })
  },patchReplys(reply,id){
   
    // this.state.Posts[id-1].reply=reply
    // var replyForm=new FormData();
    // replyForm.append("id",id);
    // replyForm.append("reply",reply);
    axios({
      url: 'http://8.130.46.21:8000/reply/add/',
      method: 'post',
      data: reply
    }).then(res=>{
      ElMessage({
        message: "评论成功",
        type: "success",
      });
    }
    ).catch(res=>{
      ElMessage({
        message: "评论失败，请重试",
        type: "error",
      });
    })
  },deletePost(id){
    
    var deleteForm=new FormData();
    deleteForm.append("id",id);
    axios({
      url: 'http://8.130.46.21:8000/post/delete/',
      method: 'post',
      data: deleteForm
    }).then(res=>{
      ElMessage({
        message: "删帖成功",
        type: "success",
      });
    }
    ).catch(res=>{
      ElMessage({
        message: "删帖失败，请重试",
        type: "error",
      });
    })
  },
  // // 获取志愿者
  // getVolunteer() {
  //   axios({
  //     url: 'http://192.168.53.171:3000/volunteer/1', //志愿者
  //     // url: 'http://192.168.2.108:3000/volunteer/'+id, //志愿者
  //     method: 'get',
  //   }).then(res => {
  //     this.state.volunteer = res.data;
  //     console.log("res.data");
  //   })
  // },
  getPolicy(id) {
  return  axios({
    //  url: 'http://localhost:3000/searchMsg/',
      url: 'http://192.168.53.171:3000/searchMsg/',
      method: 'get',
    })
  },
  getVolunteer(id) {
    return  axios({
      //  url: 'http://localhost:3000/volunteer/',
        url: 'http://192.168.53.171:3000/volunteer/',
        method: 'get',
      })
    },
  
};

export default store;
